cmake_minimum_required(VERSION 3.15.0)

project(implicit1D LANGUAGES Fortran)

if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
  set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -ffree-line-length-none -ffpe-trap=invalid,zero,overflow")
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -O0 -Wall -freport-bug -fcheck=all -g -fbacktrace -ffpe-trap=invalid,zero,overflow")
  else ()
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Ofast")
  endif ()
  # FIXME: OpenMP causes sigmentation faults.
  # set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fopenmp")
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fpe0 -no-wrap-margin -O0 -check bounds -g -traceback -qmkl")
  else ()
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -no-wrap-margin -O3 -align array64byte -qmkl")
  endif ()
endif ()

if (DEFINED ENV{NETCDF} AND (NOT DEFINED ENV{NETCDF_ROOT}))
  set(ENV{NETCDF_ROOT} $ENV{NETCDF})
endif()
if (DEFINED ENV{NETCDF_ROOT})
  include_directories("$ENV{NETCDF_ROOT}/include")
  link_directories("$ENV{NETCDF_ROOT}/lib")
else()
  find_package(PkgConfig)
  if (PKG_CONFIG_FOUND)
    pkg_search_module(NETCDF REQUIRED netcdf)
    if (NETCDF_FOUND)
      include_directories(${NETCDF_INCLUDEDIR})
      link_directories(${NETCDF_LIBDIR})
    else ()
      message(FATAL_ERROR "Pkg-config could not fine netcdf library!")
    endif ()
  else ()
    message(FATAL_ERROR "Unable to find pkg-config library!")
  endif ()
endif()

if (CMAKE_Fortran_COMPILER_ID MATCHES Intel)
  set(EXTERNAL_LIBS netcdff netcdf)
elseif (CMAKE_Fortran_COMPILER_ID MATCHES GNU)
  set(EXTERNAL_LIBS netcdff)
endif ()

if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
  find_package(LAPACK REQUIRED)
  if (DEFINED ENV{LAPACK} AND (NOT DEFINED ENV{LAPACK_ROOT}))
    set(ENV{LAPACK_ROOT} $ENV{LAPACK})
  endif ()
  if (DEFINED ENV{LAPACK_ROOT})
    message(STATUS "Found LAPACK in $ENV{LAPACK_ROOT}.")
    add_definitions(-DHAS_LAPACK)
    set(HAS_LAPACK TRUE)
    include_directories("$ENV{LAPACK_ROOT}/include")
    if (EXISTS "$ENV{LAPACK_ROOT}/lib")
      link_directories("$ENV{LAPACK_ROOT}/lib")
    endif ()
    if (EXISTS "$ENV{LAPACK_ROOT}/lib64")
      link_directories("$ENV{LAPACK_ROOT}/lib64")
    endif ()
    list(APPEND EXTERNAL_LIBS "-framework Accelerate")
  else()
    find_library(LAPACK_LIB NAMES lapack)
    find_library(BLAS_LIB NAMES blas)
    if(LAPACK_LIB AND BLAS_LIB)
      list(APPEND EXTERNAL_LIBS ${LAPACK_LIB} ${BLAS_LIB})
    else()
      list(APPEND EXTERNAL_LIBS "-framework Accelerate")
    endif()
  message(STATUS "EXTERNAL_LIBS after adding LAPACK: ${EXTERNAL_LIBS}")
  endif ()
endif ()

set(adv_lib
  src/constants_mod.F90
  src/time_mod.F90
  src/namelist_mod.F90
  src/time_stepping_mod.F90
  src/weno_mod.F90
  src/upwind_mod.F90
  src/finite_diff_mod.F90
  src/MCV_mod.F90
  src/TVD_mod.F90
  src/boundary_mod.F90
  src/adv_1d_test_mod.F90
  src/butcher_tableau_mod.F90
  src/history_mod.F90
  src/diagnostics_mod.F90)

add_library(adv_lib ${adv_lib})
target_link_libraries(adv_lib ${EXTERNAL_LIBS})

add_executable(main.exe src/main.F90)
target_link_libraries(main.exe adv_lib)
